package com.dianganjiao.lib.base

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.dianganjiao.lib.util.KeyBoardUtil
import com.tencent.bugly.beta.Beta

/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */
abstract class AbsActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setWindowStatusBar()
        setContentView(getContainerView(getCreateContentViewId()))
        onCreateState(savedInstanceState)
        onCreateViewChanged()
    }


    override fun onResume() {
        super.onResume()
        if(isCheckUpgrade()){
            Beta.checkUpgrade(false,true)
        }
    }

    open fun isCheckUpgrade():Boolean{
        return true
    }


    open fun onCreateState(savedInstanceState: Bundle?){

    }

   open fun getContainerView(layoutId:Int):View{
        return View.inflate(this,layoutId,null)
    }


    abstract fun getCreateContentViewId():Int


    abstract fun onCreateViewChanged()


    internal fun setWindowStatusBar(){
        //状态栏透明化: 侵入式透明status bar
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            if (isStatusBarWhite()) {
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            } else {
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = 0
        }
    }

    open fun isStatusBarWhite(): Boolean {
        return false
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
            KeyBoardUtil.hideSoftInput(window.decorView)
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            KeyBoardUtil.releaseObserver(this)
        }
    }
}